﻿using System;

using VSTalk.Tools;

namespace VSTalk.Engine.Core.Event
{
    public class EventFlow : IEventFlow
    {
        private static IEventAggregator EventBus
        {
            get { return AppServices.Get<IEventAggregator>(); }
        }

        public void Setup<TFrom>(ITransactionUnit<TFrom> unit) where TFrom : class
        {
            EventBus.GetEvent<TFrom>().Subscribe(@from =>
            {
                foreach (var tranUnit in unit.Transact(@from))
                {
                    EventBus.GetType().GetMethod("Publish")
                        .MakeGenericMethod(tranUnit.GetType())
                        .Invoke(EventBus, new []{tranUnit});
                }
            });
        }
    }
}